class TimeUtils {
  static String getTimeStrFromSS(int seconds) {
    int hours = seconds ~/ 3600;
    int minutes = (seconds % 3600) ~/ 60;
    int remainingSeconds = seconds % 60;
    String formattedTime = '';
    if (hours > 0) {
      formattedTime += '$hours小时 ';
    }
    if (minutes > 0) {
      formattedTime += '$minutes分钟 ';
    }
    formattedTime += '$remainingSeconds秒';
    return formattedTime;
  }

  static String getCurrentTimestamp() {
    DateTime now = DateTime.now();
    int timestamp = now.toUtc().millisecondsSinceEpoch ~/ 1000;
    return timestamp.toString();
  }

  ///获取当前时间的毫秒级时间戳
  static int getNowTimestamp() {
    return DateTime.now().millisecondsSinceEpoch ~/ 1000;
  }

  /// 将秒数转成分秒
  static String formatDuration(int seconds) {
    if (seconds <= 0) {
      return '0分0秒';
    }

    int minutes = seconds ~/ 60;
    int remainingSeconds = seconds % 60;

    String minutesString = minutes.toString().padLeft(2, '0');
    String secondsString = remainingSeconds.toString().padLeft(2, '0');

    return '$minutesString 分 $secondsString 秒';
  }

  ///
  static String iMConversationshowTime(int msglastTime, bool showDetail) {
    if (msglastTime == 0) return '异常';
    // 今天的时间
    DateTime nowDate = DateTime.now();
    DateTime msgDate =
        DateTime.fromMillisecondsSinceEpoch((msglastTime * 1000).round());

    String result;
    Duration difference = nowDate.difference(msgDate);

    if (difference.inDays == 0) {
      // 同一天，显示时间
      result =
          "${getPeriodOfTime(msgDate.hour, msgDate.minute)} ${msgDate.hour}:${msgDate.minute}";
    } else if (difference.inDays == 1) {
      // 昨天
      result = showDetail
          ? "昨天 ${getPeriodOfTime(msgDate.hour, msgDate.minute)} ${msgDate.hour}:${msgDate.minute}"
          : "昨天";
    } else if (difference.inDays == 2) {
      // 前天
      result = showDetail
          ? "前天 ${getPeriodOfTime(msgDate.hour, msgDate.minute)} ${msgDate.hour}:${msgDate.minute}"
          : "前天";
    } else if (difference.inDays < 7) {
      // 一周内
      String weekDay = weekdayStr(msgDate.weekday);
      result = showDetail
          ? "$weekDay ${getPeriodOfTime(msgDate.hour, msgDate.minute)} ${msgDate.hour}:${msgDate.minute}"
          : weekDay;
    } else {
      // 显示日期
      result = showDetail
          ? "${msgDate.year}-${msgDate.month}-${msgDate.day} ${getPeriodOfTime(msgDate.hour, msgDate.minute)} ${msgDate.hour}:${msgDate.minute}"
          : "${msgDate.year}-${msgDate.month}-${msgDate.day}";
    }

    return result;
  }

  static String getPeriodOfTime(int time, int minute) {
    int totalMin = time * 60 + minute;
    String showPeriodOfTime = "";

    if (totalMin > 0 && totalMin <= 5 * 60) {
      showPeriodOfTime = "凌晨";
    } else if (totalMin > 5 * 60 && totalMin < 12 * 60) {
      showPeriodOfTime = "上午";
    } else if (totalMin >= 12 * 60 && totalMin <= 18 * 60) {
      showPeriodOfTime = "下午";
    } else if ((totalMin > 18 * 60 && totalMin <= (23 * 60 + 59)) ||
        totalMin == 0) {
      showPeriodOfTime = "晚上";
    }

    return showPeriodOfTime;
  }

  static String weekdayStr(int dayOfWeek) {
    Map<int, String> daysOfWeekDict = {
      1: "星期日",
      2: "星期一",
      3: "星期二",
      4: "星期三",
      5: "星期四",
      6: "星期五",
      7: "星期六",
    };

    return daysOfWeekDict[dayOfWeek] ?? "";
  }
}
